package com.jymj.controller;

import com.jymj.entity.RenovatedFile;
import com.jymj.entity.YlEntity;
import com.jymj.entity.dto.RenovatedDto;
import com.jymj.service.RenovatedService;
import com.jymj.service.YlService;
import com.jymj.utils.PageUtils;
import com.jymj.utils.Query;
import com.jymj.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/yl")
public class YlController extends AbstractController {

    @Autowired
    private YlService ylService;
    @Autowired
    private RenovatedService renovatedService;

    @RequestMapping("/queryList")
    @ResponseBody
    public R queryList(@RequestParam Map<String, Object> params) {
        String code = null;
        if (params.get("code") != null){
            code =  params.get("code").toString();
        }
        Query query = new Query(params);
        List<YlEntity> ylEntities = ylService.queryList(query);
        int total = ylService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(ylEntities, total, query.getLimit(), query.getPage());
        return R.ok().put("data", pageUtil);
    }


    @RequestMapping("/queryByPoint")
    @ResponseBody
    public R queryByPoint(@RequestParam("point") String point, Integer pjid) {
        YlEntity ylEntity = ylService.queryByPoint(point, pjid);
        return R.ok().put("data", ylEntity);
    }

    /**
     * 院落整改图片上传
     * @param file
     * @return
     */
    @PostMapping("/uploadFile")
    public R uploadFile(MultipartFile file) throws FileNotFoundException {
        RenovatedFile renovatedFile = renovatedService.uploadFile(file);
        return R.ok().put("data", renovatedFile);
    }

    /**
     * 院落整改信息提交（添加修改）
     * @param renovatedDto
     * @return
     */
    @PostMapping("/RenovatedSaveOrUpdate")
    public R saveOrUpdate(@RequestBody RenovatedDto renovatedDto){
        renovatedService.saveOrUpdate(renovatedDto);
        return R.ok("提交成功");
    }

    /**
     * 批量删除图片
     * @param ids
     * @return
     */
    @PostMapping("/deleteFile")
    public R deleteFile(@RequestBody Long... ids){
        renovatedService.deleteFile(ids);
        return R.ok("删除成功");
    }

    /**
     * 删除翻建点位及图片
     * @param renovatedDto
     * @return
     */
    @PostMapping("/deleteRenovated")
    public R deleteRenovated(@RequestBody RenovatedDto renovatedDto){
        renovatedService.deleteRenovated(renovatedDto);
        return R.ok("删除成功");
    }

    /**
     * 翻建点查询
     * @param point
     * @return
     */
    @PostMapping("/getRenovated")
    public R getRenovated(String point){
        RenovatedDto renovateds = renovatedService.getRenovated(point);
        return R.ok().put("data",renovateds);
    }

    /**
     * 根据code xzqmc查询翻建点位
     * @param code
     * @param xzqmc
     * @return
     */
    @PostMapping("/getRenovatedList")
    public R getRenovatedList(String code,String xzqmc,Long userId,Integer qutype){
        List<RenovatedDto> renovatedDtoList = renovatedService.getRenovatedList(code,xzqmc,userId, null,qutype);
        List<RenovatedDto> renovatedList = renovatedService.getRenovatedList(code, xzqmc,null, userId,qutype);
        renovatedDtoList.addAll(renovatedList);
        return R.ok().put("data", renovatedDtoList);
    }
}
